package com.unionftech.content_user.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.launcher.ARouter
import com.unionftech.common.R.*
import com.unionftech.common.base.BaseFragment
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.router.service.ILoginService
import com.unionftech.common.utils.ActivityCollector
import com.unionftech.common.utils.SpUtils
import com.unionftech.content_user.R
import com.unionftech.content_user.ui.dialog.UFLogoutDialog


/**
 * 我的
 */
class MineFragment : BaseFragment() {
    private val mLogout by lazy { mRootView.findViewById<TextView>(R.id.logout) }

    @Autowired
    @JvmField
    var mLoginService: ILoginService? = null

    init {
        ARouter.getInstance().inject(this)
    }

    companion object {
        @JvmStatic
        fun newInstance() = MineFragment().apply {
            arguments = Bundle().apply {}
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {}
    }


    override fun inflaterContentView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_mine, container, false)
    }

    override fun setView() {
        super.setView()
        hideTitleBar()
        mLogout.setOnClickListener {
            showLogoutDialog()
        }
    }

    private fun showLogoutDialog() {
        UFLogoutDialog(mContext).setTitle(
            DynamicResourceManager.getString(
                mContext, string.confirm_logout_tip
            )
        ).setCancelText(DynamicResourceManager.getString(mContext, string.cancel))
            .setConfirmText(DynamicResourceManager.getString(mContext, string.confirm))
            .setCallBack { result ->
                if (result) {
                    requestLogout()
                }
            }.show()
    }

    private fun requestLogout() {
        mLoginService?.also { service ->
            showLoading()
            service.logout {
                hideLoading()
                SpUtils.getInstance().remove("login_way")
                service.gotoLogin()
                ActivityCollector.finishAll()
            }
        }
    }
}